﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    [Route("category")]
    public class CategoryController : PageController
    {
       [HttpGet]
        public HttpResponseMessage Get()
        {
            string sql = "select category_id as Id,category_Name as CategoryName,category_AddTime as Time from category_tb";
            SQLCommand sc = new SQLCommand(sql);
            DataTable dt = sc.ExecuteScalars();
            if (dt == null)
                return AssistFun.toJson(new { status = 201, tip = "暂时没有数据" });
            else
            {
                List<ShowCategoryInfo> categories = AssistFun.DataTableToList<ShowCategoryInfo>(dt);
                return AssistFun.toJson(new { status = 200, tip = "成功",data=new {tatal=categories.Count,rows=categories } });
            }            
        }

        [HttpPost]
        public HttpResponseMessage Post(dynamic obj)
        {
            if (obj == null)
                return AssistFun.toJson(new { status = 201, tip = "请提供有效信息" });
            else
            {
                string name = obj.CategoryName.ToString();
                name = name.Replace(" ", "");
                int loginid = GetCrrentIDByToken();
                if(loginid ==0)
                    return AssistFun.toJson(new { status = 201, tip = "请确保你是有效用户" });
                if(CheckCategoryName(name))
                    return AssistFun.toJson(new { status = 201, tip = "该品类名称已经存在" });
                int cid = GetNewSafeID(loginid);
                string sql = "insert into Category_tb(category_id,category_Name) values(@categoryId,@Cname)";
                SqlParameter[] paras = new SqlParameter[] {new SqlParameter("@categoryId",cid),new SqlParameter("@Cname",name) };
                SQLCommand sc = new SQLCommand(sql);
                sc.AddInsert("@categoryId", cid);
                sc.AddInsert("@Cname", name);
                int res = sc.ExecuteNonQuery();
                if(res==0)
                    return AssistFun.toJson(new { status = 201, tip = "请确保你是有效用户" });
                else
                    return AssistFun.toJson(new { status = 200, tip = "成功" });
            }
        }

        // PUT: api/Category/5 
        public void Put(int id, [FromBody]string value)
        {
        }

        [HttpDelete]
        public HttpResponseMessage Delete(ShowCategoryInfo category)
        {
            if (CheckStyleTypename(category.CategoryName.Replace(" ","")))
                return AssistFun.toJson(new { status = 201, tip = "品类“" + category.CategoryName + "”已经被使用，无法删除" });
            else
            {
                List<SQLCommand> cmds = new List<SQLCommand>();
                string sql = "delete from Category_tb where category_id=@categoryId";
                SQLCommand sc = null;
                sc = new SQLCommand(sql, "@categoryId", category.Id);
                cmds.Add(sc);
                sql = "delete from CategoryUser_tb where category_id=@categoryId";
                sc = new SQLCommand(sql, "@categoryId", category.Id);
                cmds.Add(sc);
                int res = SQLCommand.ExecTran(cmds) ;
                if (res == 0)
                    return AssistFun.toJson(new { status = 201, tip = "失败" });
                else
                    return AssistFun.toJson(new { status = 200, tip = "成功" });
            }
        }

        /// <summary>
        /// 查询服装品类是否存在
        /// </summary>
        /// <param name="styleName"> 品类名称</param>
        /// <returns></returns>
        private bool CheckStyleTypename(string typeName)
        {
            try
            {
                string sql = "select type from Style_tb where type=@name";
                SQLCommand sc = new SQLCommand(sql, "@name", typeName);
                if (sc.ExecuteScalar() == null)
                    return false;
                else
                    return true;
            }
            catch (Exception e)
            {
                return true;
            }
        }
    }
}
